<html>
<head><meta charset="utf-8"><title>A few small questions about traits · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html">A few small questions about traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206470149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206470149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206470149">(Aug 10 2020 at 14:56)</a>:</h4>
<p>Heya! Ive got a few questions based on this code example: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=cda097b62e330900184a09db2b835ab9">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=cda097b62e330900184a09db2b835ab9</a></p>
<p>My question is whether there is any way to sort of guide the compiler into understanding that <code>B1</code> implements <code>OrBit&lt;B0&gt;</code> simply because both types essentially boil down to either of two structs (<code>True</code> or <code>False</code>) and <code>OrBit</code> is implemented for all combinations of those structs. All of this without having to add a type restriction in the <code>impl SomeOperation...</code>?</p>



<a name="206470280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206470280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206470280">(Aug 10 2020 at 14:57)</a>:</h4>
<p>im not quite sure if something like this is possible, but id assume that rustc could infer that my code is valid without having to be more explicit.</p>



<a name="206470912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206470912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206470912">(Aug 10 2020 at 15:01)</a>:</h4>
<p>I don't think you can tell the compiler that the set of types that implement a trait is closed. But maybe there's a different way to achieve that since you can <a href="https://sdleffler.github.io/RustTypeSystemTuringComplete/">definitely</a> do type-level computation.</p>



<a name="206471426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206471426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206471426">(Aug 10 2020 at 15:05)</a>:</h4>
<p>right so what im trying to do is plainly impossible. Alright thanks a lot!</p>



<a name="206471527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206471527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206471527">(Aug 10 2020 at 15:06)</a>:</h4>
<p>Well, see above</p>



<a name="206474767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206474767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206474767">(Aug 10 2020 at 15:30)</a>:</h4>
<p>right i see, i mean what im trying to do could work, but the problem is that in a bigger PoC as you get more complex operations type bounds become complex, and youd need many of them. Anyway, thanks a lot</p>



<a name="206475078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206475078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206475078">(Aug 10 2020 at 15:33)</a>:</h4>
<p>Maybe you can use <code>const fn</code> instead?</p>



<a name="206475222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206475222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206475222">(Aug 10 2020 at 15:34)</a>:</h4>
<p>haha i could, but the challenge is to implement 8bit numbers + arithmetic at the type system level</p>



<a name="206475556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206475556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206475556">(Aug 10 2020 at 15:37)</a>:</h4>
<p>Like the TypeNum crate?</p>



<a name="206475861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206475861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206475861">(Aug 10 2020 at 15:40)</a>:</h4>
<p>i suppose, if im being honest im not quite sure what exactly the number interpretation in typenum looks like, but im sorta building off of something like this </p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">U8</span><span class="o">&lt;</span><span class="n">B7</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B6</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B5</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B4</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B3</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B2</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B1</span>: <span class="nc">Bit</span><span class="p">,</span><span class="w"> </span><span class="n">B0</span>: <span class="nc">Bit</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">_bits</span>: <span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B7</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B6</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B5</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B4</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B3</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B2</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B1</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">B0</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Everything seemed like it was going well till i got to addition :(</p>



<a name="206511202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/206511202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Areredify <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#206511202">(Aug 10 2020 at 20:46)</a>:</h4>
<p>In chalk we don't have a notion of closed types, at least yet</p>



<a name="207049812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207049812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Ross <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207049812">(Aug 16 2020 at 02:08)</a>:</h4>
<p><span class="user-mention" data-user-id="328945">@Valerian G.</span> I think you should be able to get _something_ like this working with something like:</p>
<div class="codehilite"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">OrBit</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Output</span>: <span class="nc">Bit</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="c1">// ...</span>

<span class="k">trait</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">Self</span><span class="o">&gt;</span>: <span class="nc">OrBit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="cm">/* ... other bit traits&lt;T&gt; ... */</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span>: <span class="nc">OrBit</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="cm">/* ... other bit traits&lt;T&gt; ... */</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">B1</span>: <span class="nc">Bit</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="n">B0</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">B0</span>: <span class="nc">Bit</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Bit</span><span class="o">&lt;</span><span class="n">B1</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">SomeOperation</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Bits2</span><span class="o">&lt;</span><span class="n">B1</span><span class="p">,</span><span class="w"> </span><span class="n">B0</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</code></pre></div>


<p>?</p>
<p>It's a bit more boilerplate heavy, but it should mean <code>Bit</code> really encodes the notion of "I can do any bitwise operation with this type, and I can use the result to do any other bitwise operation again"</p>
<p>It still won't encode a fixed number of types, but it should practically allow writing code (at the cost of N^2 where clauses for N bits)?</p>



<a name="207049831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207049831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Ross <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207049831">(Aug 16 2020 at 02:09)</a>:</h4>
<p>Right now, your <code>Bit</code> trait doesn't actually encode anything, so if you have <code>T: Bit</code> I can just impl <code>Bit</code> on anything and then throw it in, and your code has to deal with that. But if you had <code>Bit</code> be directed, and represent "I can do bitwise operations with this type", you should be able to do at least some math with it?</p>



<a name="207049944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207049944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Ross <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207049944">(Aug 16 2020 at 02:12)</a>:</h4>
<p>Though I suspect even that will probably fall apart sooner or later if you have too many operations, since the <code>Bit</code> trait still doesn't represent being able to do anything with True and False in particular</p>



<a name="207078735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078735">(Aug 16 2020 at 16:47)</a>:</h4>
<p>yeah thats exactly the problem, to put it in perspective, my code started failing when i had a complex operation rely on recursive shifting where you would shift a number represented by 8bits then or it with another number so on until a condition is met. The problem was that the type constraint that you had to add was essentially all the possible permutations of <code>Or</code> between bits 0 through 7</p>



<a name="207078739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078739">(Aug 16 2020 at 16:47)</a>:</h4>
<p>and thats for a simple (yet somewhat complex) operation</p>



<a name="207078741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078741">(Aug 16 2020 at 16:47)</a>:</h4>
<p>it gets a lot uglier when you try to do addition</p>



<a name="207078798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078798">(Aug 16 2020 at 16:48)</a>:</h4>
<p>i suppose this problem will stay until we have sealed traits in rust and when the type system is able to take note of sealed traits</p>



<a name="207078833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078833">(Aug 16 2020 at 16:50)</a>:</h4>
<p>although i think your solution might make it a lot less complicated, since i just need to make sure that each bit implements <code>Bit</code> for every other <code>Bit</code> in the number i suppose, im sorry if im not making any sense</p>



<a name="207078911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207078911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207078911">(Aug 16 2020 at 16:51)</a>:</h4>
<p>ill give it a try again, perhaps my little experiment will get a little further this time!</p>



<a name="207083262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/A%20few%20small%20questions%20about%20traits/near/207083262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Valerian G. <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/A.20few.20small.20questions.20about.20traits.html#207083262">(Aug 16 2020 at 18:34)</a>:</h4>
<p>Yeah just gave it a small try: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=04dec6909d54584da987257adb493b05">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=04dec6909d54584da987257adb493b05</a><br>
We still have to build the constraints for every single possibility, thus when operations get more complicated or nested it wont compile. :(</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>